package com.pxst.fi.controller;

import com.pxst.annotations.LogOperate;
import com.pxst.constants.ResCodeContants;
import com.pxst.fi.entity.SysThirdPartySettingEntity;
import com.pxst.fi.req.ThirdPaySettingReq;
import com.pxst.fi.req.ThirdPaySettingUpdateReq;
import com.pxst.fi.req.TpiDownloadReq;
import com.pxst.fi.resp.ThirdPaySettingResp;
import com.pxst.fi.service.SysThirdPartyConfigService;
import com.pxst.fi.service.SysThirdPartySettingService;
import com.pxst.resp.ApiResp;
import com.pxst.resp.PageResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.ValidationException;


/**
 * @author rj
 * @create 2023-09-22-16:40
 */
@Api(tags = "第三方支付接口")
@RestController
@RequestMapping("/thirdPay")
public class ThirdPayController {

    @Autowired
    private SysThirdPartySettingService thirdPartySettingService;

    @Autowired
    private SysThirdPartyConfigService thirdPartyConfigService;

    @GetMapping("/global/vnpay")
    @ApiOperation(value = "查询第三方支付设定")
    public ApiResp<PageResp<ThirdPaySettingResp>> getThirdPaySetting(ThirdPaySettingReq thirdPaySettingReq) {
        return thirdPartySettingService.getThirdPaySetting(thirdPaySettingReq);
    }

    @PutMapping("/global/vnpay")
    @ApiOperation(value = "修改第三方支付设定")
    @LogOperate(operate = "修改第三方支付设定", service = SysThirdPartySettingService.class, type = "edit")
    public ApiResp updateThirdPaySetting(@Validated ThirdPaySettingUpdateReq thirdPaySettingUpdateReq) {
        Integer status = thirdPaySettingUpdateReq.getStatus();
        if (!ObjectUtils.isEmpty(status) && status != 0 && status != 1){
            throw new ValidationException("状态必须为 0 或 1");
        }
        if (thirdPaySettingUpdateReq.getThirdPartyMin().compareTo(thirdPaySettingUpdateReq.getThirdPartyMax()) > 0){
            throw new ValidationException("第三方限制最小值不能大于最大值");
        }
        if (thirdPaySettingUpdateReq.getTradeMin().compareTo(thirdPaySettingUpdateReq.getTradeMax()) > 0){
            throw new ValidationException("预设单笔最小金额不能大于最大金额");
        }
        SysThirdPartySettingEntity thirdPartySetting = new SysThirdPartySettingEntity();
        BeanUtils.copyProperties(thirdPaySettingUpdateReq, thirdPartySetting);
        boolean update = thirdPartySettingService.updateById(thirdPartySetting);
        if (!update) {
            return ApiResp.error(ResCodeContants.BUS_ERROR, "修改第三方支付设定失败");
        }
        return ApiResp.sucess();
    }

    @GetMapping("/history/download")
    @ApiOperation(value = "汇出报表")
    public ApiResp download(@Validated TpiDownloadReq tpiDownloadReq){

        return thirdPartyConfigService.download(tpiDownloadReq);
    }
}
